package ed.hbut.supermarket1.service.impl;


import ed.hbut.supermarket1.entity.Goods;
import ed.hbut.supermarket1.mapper.GoodsMapper;
import ed.hbut.supermarket1.mapper.SupplierMapper;
import ed.hbut.supermarket1.service.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    public SupplierMapper supplierMapper;

    @Autowired
    public GoodsMapper goodsMapper;

    @Override
    public void addGoods(Goods goods) {

        //获取供应商名称
        String name= supplierMapper.getNameById(goods.getSupplierSupplierID());
        goods.setGoodsSupplierName(name);

        goodsMapper.addGoods(goods);

    }

    @Override
    public List<Goods> getGoods() {

        return goodsMapper.getGoods();
    }

    @Override
    public Goods getGoodsById(int id) {
        return goodsMapper.getGoodsById(id);
    }

    @Override
    public void updateGoods(int id, Goods goods) {

        goods.setGoodsSupplierName(supplierMapper.getNameById(goods.getSupplierSupplierID()));
        log.info("update goods :{}",goods);
        goods.setGoodsID(id);
        goodsMapper.updateGoods(goods);
    }

    @Override
    public void deleteGoods(int id) {

       Goods goods= goodsMapper.getGoodsById(id);
       log.info(goods.toString());
       if(goods.getGoodsInventory()==0){
           goodsMapper.deleteGoods(id);
       }

    }
}
